package cn.labbook.bluetooth.service.impl;

import cn.labbook.bluetooth.constant.CommonConstant;
import cn.labbook.bluetooth.entity.Constant;
import cn.labbook.bluetooth.entity.User;
import cn.labbook.bluetooth.mapper.UserMapper;
import cn.labbook.bluetooth.pojo.dto.UserDTO;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.IApplyRecordService;
import cn.labbook.bluetooth.service.IUserService;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lonzy
 * @since 2019-03-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * 微信收款二维码
     */
    private static final int TYPE_WEIXIN = 1;
    /**
     * 支付宝收款二维码
     */
    private static final int TYPE_ALI = 1;

    @Autowired
    private IApplyRecordService applyRecordService;
    @Autowired
    private IAmmeterService ammeterService;


    @Override
    public void updateQrCode(String userId, String qrCodePath, Integer type) {
        User user = getById(userId);
        if (Objects.isNull(user)) {
            return;
        }

        if (Objects.equals(TYPE_WEIXIN, type)) {
            user.setQrCodeWeixin(qrCodePath);
        } else {
            user.setQrCodeAli(qrCodePath);
        }
        updateById(user);
    }

    @Override
    public UserDTO getUser(String userId) {
        User user = getById(userId);
        if (Objects.isNull(user)) {
            return new UserDTO();
        }
        UserDTO dto = new UserDTO();
        dto.setQrCodeWeixin(user.getQrCodeWeixin());
        dto.setQrCodeAli(user.getQrCodeAli());

        Integer noticeReadFlag = user.getNoticeReadFlag();
        if (Objects.isNull(noticeReadFlag)) {
            noticeReadFlag = CommonConstant.NO;
        }
        dto.setNoticeReadFlag(noticeReadFlag);

        int existInApplyFlag = CommonConstant.NO;
        if (applyRecordService.existIn(user.getAdminId())) {
            existInApplyFlag = CommonConstant.YES;
        }
        dto.setExistInApplyFlag(existInApplyFlag);

        int upQrCodeFlag = CommonConstant.NO;
        if (StringUtils.isBlank(user.getQrCodeAli()) && StringUtils.isBlank(user.getQrCodeWeixin()) && ammeterService.isLandlord(user.getAdminId())) {
            upQrCodeFlag = CommonConstant.YES;
        }
        dto.setUpQrCodeFlag(upQrCodeFlag);

        return dto;
    }


    @Override
    public void readNotice(String userId) {
        User user = getById(userId);
        if (Objects.isNull(user)) {
            return;
        }
        user.setNoticeReadFlag(CommonConstant.YES);
        updateById(user);
    }

    @Override
    public String getMobilePhone(String adminId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAdminId, adminId);
        User user = getOne(wrapper);
        if (Objects.isNull(user)) {
            return null;
        }
        return user.getMobilephone();
    }

    @Override
    public User getByAdminId(String adminId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAdminId, adminId);
        User user = getOne(wrapper);
        if (Objects.isNull(user)) {
            return new User();
        }
        return user;
    }
}
